#include "flowcontroler.h"

#include "Interfaces/xmysql.h"
#include "Interfaces/xtcpinterface.h"
#include "biseserver.h"

#include <QApplication>
#include <QDebug>
#include <QMessageBox>

#define IP "127.0.0.1"
#define PORT 3306
#define DB_NAME "Anime"
#define USER "root"
#define PSWD "123456"

#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8888

#define METHOD 1

XInterface *FlowControler::Interface = nullptr;
FlowControler::FlowControler()
{
}

int FlowControler::run(int argc, char *argv[])
{
    QApplication a(argc, argv);
    bool         logout = true;
    int          ret    = 0;

    //    qDebug() << getNoRepeatPingyinForWord("张三");


#if METHOD
    // 数据库连接
    XMysql *mysql = new XMysql();
    Interface = mysql;
    if ( !mysql->connectToDB(IP, PORT, DB_NAME, USER, PSWD) )
    {
        qCritical() << "数据库连接失败！";
        // 添加用户确定选项
        QMessageBox::StandardButton btn = QMessageBox::question(nullptr, "提示", "数据库连接失败，是否继续使用软件？");
        if ( btn != QMessageBox::Yes )
        {
            return 0;
        }
    }
    else
    {
        qInfo() << "数据库连接成功！";
    }
#else
    XTcpInterface *tcp = new XTcpInterface();
    Interface = tcp;
    if ( !tcp->connectToServer(SERVER_IP, SERVER_PORT) )
    {
        qCritical() << "服务器连接失败！";
        // 添加用户确定选项
        QMessageBox::StandardButton btn = QMessageBox::question(nullptr, "提示", "数据库连接失败，是否继续使用软件？");
        if ( btn != QMessageBox::Yes )
        {
            return 0;
        }
    }
    else
    {
        qInfo() << "服务器连接成功！";
    }
#endif

    // 使用循环完成注销功能,这样的好处是所有的对象都是重新创建，不需要自己去手动恢复主界面到初始状态
    BiseServer server;
    server.show();


    ret = a.exec();
    qDebug() << "程序退出";

    return ret;
}
